home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Education / RLaB / examples / euclid.r < prev    next >
Encoding:
Text File  |  1994-02-21  |  492 b   |  29 lines  |  [TEXT/ttxt]

  1. //-------------------------------------------------------------------//
  2. //  Euclid's algorithm
  3. //
  4.  
  5. //  Syntax:    euclid ( M , N )
  6.  
  7. //  Description:
  8.  
  9. //  Euclid's algorithm computes the greatest common divisor of two
  10. //  integers (M, and N).
  11. //-------------------------------------------------------------------//
  12.  
  13. euclid = function ( M , N )
  14. {
  15.   local( m , n , r );
  16.  
  17.   m = M;
  18.   n = N;
  19.   r = 1;
  20.   while(r)
  21.   {
  22.     r = mod(m,n);
  23.     if(r == 0) { break; }
  24.     m = n;
  25.     n = r;
  26.   }
  27.   return n;
  28. };
  29.